<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Discount_model extends MY_Model {
    
    protected $TB_DISCOUNT = 'dc_discount';
    protected $DISCOUNT_COLS = array(
        'id',
        'product_id',
        'rate',
        'vote_start_time',
        'vote_end_time'
    );
    
    public function __construct() {
        parent::__construct();
    }
    
    public function get_discount($discount_id, $cols=array()) {
        $cols = $cols ? $cols : $this->DISCOUNT_COLS;
        $cols_sql = join(', ', $cols);
        return $this->db->query("SELECT $cols_sql FROM $this->TB_DISCOUNT
            WHERE id = $discount_id LIMIT 1")->row();
    }
    
    public function del_discount($discount_id) {
        return $this->db->query("DELETE FROM $this->TB_DISCOUNT 
            WHERE id = $discount_id LIMIT 1");
    }
    
    public function add_discount($product_id, $rate, $vote_start_time=null, 
        $vote_end_time=null) {
        return $this->db->query("INSERT INTO $this->TB_DISCOUNT 
            (`product_id`, `rate`, `vote_start_time`, `vote_end_time`) VALUES 
            ('$product_id', '$rate', '$vote_start_time', '$vote_end_time')") ?
            $this->db->insert_id() : false;
    }
    
    public function set_discount($discount_id, $sets) {
        $sets_sql = $this->_sets_to_str($sets, $this->DISCOUNT_COLS);
        return $this->db->query("UPDATE $this->TB_DISCOUNT SET $sets_sql 
            WHERE id = $discount_id LIMIT 1");
    }
    
}